package coupon

import (
	session2 "OnlineRetailers/controllers/admin/session"
	"OnlineRetailers/controllers/errorDeal"
	error2 "OnlineRetailers/models/errorDeal"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

type DeleteCoupon struct {
	beego.Controller
}

func (c *DeleteCoupon) DeleteCoupon() {

	defer func() {
		c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
		c.Ctx.Output.Status = 200
		c.ServeJSON()
	}()
	var addr = "/Coupon/DeleteCoupon"
	//res := resMes.GetAdminUser{}
	//user := []*Admin.User{}
	log := &error2.Log{}
	mes := &error2.Mes{}
	var err error
	o := orm.NewOrm()

	id := c.GetString("id")
	err = session2.CheckSession(c.Ctx.Request.Header["Session"], 0)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}

	//fmt.Println(key)

	num, err := o.QueryTable("Coupon").Filter("CouponID__exact", id).Delete()
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	} else if num == 0 {
		c.Data["json"] = errorDeal.ErrLightMess(mes, "400", "失败")
		return
	}

	mes.Mes = "success"
	mes.Code = "200"
	c.Data["json"] = mes
}

func DeleteClassM2M(o orm.Ormer, id string) (err error) {
	sql := "delete from `coupon_classifications` where `coupon_id`=?"
	_, err = o.Raw(sql, id).Exec()
	sql = "delete from `w_x_user_coupons` where `coupon_id`=?"
	_, err = o.Raw(sql, id).Exec()
	if err != nil {

		return
	}
	return nil
}
